package com.itheima.reggie.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private HttpSession session;

    // 员工登录
    @Override
    public ResultInfo login(String username, String password) {
        // 1.先根据用户名查询数据库
        Employee employee = employeeMapper.findByUsername(username);


        // 2.判断未查到
        if (employee == null) {
            return ResultInfo.error("此用户不存在");
        }
        // 3.比较密码
        // 3-1 将用户明文转为密文
        String md5 = SecureUtil.md5(password);
        // 3-2 比较
        if (!StrUtil.equals(md5,employee.getPassword())) {
            return ResultInfo.error("密码错误");
        }
        // 4.判断员工状态
        if (employee.getStatus()!=1) {
            return ResultInfo.error("此账号被禁用，请联系管理员");
        }
        // 5.登录成功
        session.setAttribute(Constant.SESSION_EMPLOYEE,employee);
        // 6.返回成功结果
        return ResultInfo.success(employee);
    }

    // 员工列表
    @Override
    public List<Employee> find(String name) {
        return employeeMapper.find(name);
    }

    // 新增员工
    @Override
    public void save(Employee employee) {
        // 1.补全信息
        // 1-1 雪花ID
        long employeeId = IdUtil.createSnowflake(1, 1).nextId();
        employee.setId(employeeId);
        // 1-2 初始密码
        String md5 = SecureUtil.md5(Constant.INIT_PASSWORD);
        employee.setPassword(md5);
        // 1-3 员工状态
        employee.setStatus(Employee.STATUS_ENABLE);
        // 1-4 创建修改时间
  /*      employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        // 1-5 创建、修改人
        Employee cureentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
        employee.setCreateUser(cureentEmp.getId());
        employee.setUpdateUser(cureentEmp.getId());*/

        // 2.mapper保存
        employeeMapper.save(employee);
    }

    // 回显员工
    @Override
    public Employee findById(Long id) {
        return employeeMapper.findById(id);
    }

    // 修改员工
    @Override
    public void update(Employee employee) {
        // 1.补充信息
 /*       // 1-1 修改时间
        employee.setUpdateTime(new Date());
        // 1-2 修改人
        Employee currentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
        employee.setUpdateUser(currentEmp.getId());*/


        // 2.调用mapper
       //  employeeMapper.update(employee);
        employeeMapper.updateById(employee);
    }
}
